package com.cqvip.innocence.framework.jwt.dto;

import com.cqvip.innocence.project.model.entity.Admin;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * @ClassName LoginAdmin
 * @Description TODO
 * @Author Innocence
 * @Date 2022/5/9 11:21
 * @Version 1.0
 */
@Data
@JsonIgnoreProperties({"username", "password", "enabled", "accountNonExpired", "accountNonLocked", "credentialsNonExpired", "authorities"})
public class LoginAdmin extends LoginBase implements UserDetails {
    private static final long serialVersionUID = 1L;

    /**
     * 权限和角色列表
     */
    private Set<String> permissions;

    /**
     * 用户信息
     */
    private Admin adminUser;

    private Set<? extends GrantedAuthority> authorities;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (getPermissions() == null || getPermissions().isEmpty()) {
            return null;
        } else {
            HashSet<SimpleGrantedAuthority> authorities = new HashSet<>();
            for (String item : getPermissions()) {
                authorities.add(new SimpleGrantedAuthority(item));
            }
            return authorities;
        }
    }

    @Override
    public String getPassword() {
        return this.adminUser.getPassWord();
    }

    @Override
    public String getUsername() {
        return this.adminUser.getName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
